/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapHandler;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class MessageManager
/*     */ {
/*  29 */   private static Logger logger = Logger.getLogger(MessageManager.class
/*  30 */     .getName());
/*     */ 
/*     */   public int add(String title, String message, int messageType, String attachFiles, int visible, String permissionsId, String userId, int companyId)
/*     */   {
/*  35 */     if ((title == null) || (title.trim().length() == 0)) {
/*  36 */       return -1;
/*     */     }
/*  38 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  39 */     if (conn == null) {
/*  40 */       return -1;
/*     */     }
/*     */     try
/*     */     {
/*  44 */       GenKeyQueryRunner runner = new GenKeyQueryRunner(
/*  45 */         new ScalarHandler(), new String[] { "MessageId" });
/*  46 */       runner.update(conn, "INSERT INTO T_Message_Body_Info (Title,Message,MessageType,AttachFiles,Visible,PermissionsId,CreateUserId,CompanyId,CreateDate,UpdateDate) VALUES (?,?,?,?,?,?,?,?,NOW(),NOW())", new Object[] { title, message, 
/*  47 */         Integer.valueOf(messageType), attachFiles, Integer.valueOf(visible), permissionsId, userId, 
/*  48 */         Integer.valueOf(companyId) });
/*  49 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*     */     } catch (SQLException e) {
/*  51 */       logger.warn("add message failed", e);
/*     */     } finally {
/*     */       try {
/*  54 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  56 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*  59 */     return 0;
/*     */   }
/*     */ 
/*     */   public int update(int id, String title, String message, String attachFiles, int visible, String permissionsId)
/*     */   {
/*  75 */     if (id <= 0) {
/*  76 */       return 0;
/*     */     }
/*  78 */     if ((title == null) || (title.trim().length() == 0)) {
/*  79 */       return 0;
/*     */     }
/*  81 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  82 */     if (conn == null) {
/*  83 */       return 0;
/*     */     }
/*  85 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  87 */       return runner.update(conn, "UPDATE T_Message_Body_Info SET Title = ?, Message = ?, AttachFiles = ?, Visible = ?, PermissionsId = ?, UpdateDate = NOW() WHERE MessageId = ?", new Object[] { title, 
/*  88 */         message, attachFiles, Integer.valueOf(visible), permissionsId, Integer.valueOf(id) });
/*     */     } catch (SQLException e) {
/*  90 */       logger.warn("update message failed", e);
/*     */     } finally {
/*     */       try {
/*  93 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  95 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*  98 */     return 0;
/*     */   }
/*     */ 
/*     */   public int[] addRelation(Object[][] params)
/*     */   {
/* 108 */     if ((params == null) || (params.length == 0)) {
/* 109 */       return null;
/*     */     }
/*     */ 
/* 112 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 113 */     if (conn == null) {
/* 114 */       return null;
/*     */     }
/* 116 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 118 */       return runner
/* 119 */         .batch(conn, 
/* 120 */         "INSERT INTO T_Message_Relation_Info (MessageId,ReceiveUserId,ReadFlag,CompanyId,UpdateDate) VALUES (?,?,0,?,NOW())", 
/* 122 */         params);
/*     */     } catch (SQLException e) {
/* 124 */       logger.warn("add message failed", e);
/*     */     } finally {
/*     */       try {
/* 127 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 129 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 132 */     return null;
/*     */   }
/*     */ 
/*     */   public Map<String, Object> getDetail(int id)
/*     */   {
/* 143 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 144 */     if (conn == null) {
/* 145 */       return null;
/*     */     }
/* 147 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 149 */       return (Map)runner.query(conn, "SELECT * FROM T_Message_Body_Info WHERE MessageId = ?", 
/* 150 */         new MyMapHandler(), new Object[] { Integer.valueOf(id) });
/*     */     } catch (SQLException e) {
/* 152 */       logger.warn(e);
/*     */     } finally {
/*     */       try {
/* 155 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 157 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 160 */     return null;
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> getReadedUser(int id)
/*     */     throws SQLException
/*     */   {
/* 171 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 172 */     if (conn == null) {
/* 173 */       return null;
/*     */     }
/* 175 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 177 */       return (List)runner.query(conn, "SELECT ReceiveUserId FROM T_Message_Relation_Info WHERE ReadFlag = 1 AND MessageId = ? ", 
/* 178 */         new MyMapListHandler(), new Object[] { Integer.valueOf(id) });
/*     */     } finally {
/*     */       try {
/* 181 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 183 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int updateViewStatus(int id, String userId)
/*     */   {
/* 196 */     if (id <= 0) {
/* 197 */       return 0;
/*     */     }
/* 199 */     if ((userId == null) || (userId.trim().length() == 0)) {
/* 200 */       return 0;
/*     */     }
/*     */ 
/* 203 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 204 */     if (conn == null) {
/* 205 */       return 0;
/*     */     }
/* 207 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 209 */       return runner.update(conn, "UPDATE T_Message_Relation_Info SET ReadFlag = 1, UpdateDate = NOW() WHERE MessageId = ? AND ReceiveUserId = ?", new Object[] { Integer.valueOf(id), 
/* 210 */         userId });
/*     */     } catch (SQLException e) {
/* 212 */       logger.warn("add message failed", e);
/*     */     } finally {
/*     */       try {
/* 215 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 217 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 220 */     return 0;
/*     */   }
/*     */ 
/*     */   public int delete(int id)
/*     */     throws DBException, SQLException
/*     */   {
/* 232 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 233 */     if (conn == null) {
/* 234 */       throw new DBException();
/*     */     }
/* 236 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 238 */       int result = runner.update(conn, "DELETE FROM T_Message_Body_Info WHERE MessageId = ?", Integer.valueOf(id));
/* 239 */       runner.update(conn, "DELETE FROM T_Message_Relation_Info WHERE MessageId = ?", Integer.valueOf(id));
/* 240 */       return result;
/*     */     } finally {
/*     */       try {
/* 243 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 245 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int deleteRelation(int id, String userId)
/*     */   {
/* 260 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 261 */     if (conn == null) {
/* 262 */       return 0;
/*     */     }
/* 264 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 266 */       return runner.update(conn, "DELETE FROM T_Message_Relation_Info WHERE MessageId = ? AND ReceiveUserId = ?", new Object[] { 
/* 267 */         Integer.valueOf(id), userId });
/*     */     } catch (SQLException e) {
/* 269 */       logger.warn("delete relation  failed", e);
/*     */     } finally {
/*     */       try {
/* 272 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 274 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 277 */     return 0;
/*     */   }
/*     */ 
/*     */   public int deleteRelationLogic(int id, String userId)
/*     */   {
/* 289 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 290 */     if (conn == null) {
/* 291 */       return 0;
/*     */     }
/* 293 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 295 */       return runner.update(conn, 
/* 296 */         "UPDATE T_Message_Relation_Info SET ReadFlag = 2 WHERE MessageId = ? AND ReceiveUserId = ?", new Object[] { Integer.valueOf(id), userId });
/*     */     } catch (SQLException e) {
/* 298 */       logger.warn("delete relation logic failed", e);
/*     */     } finally {
/*     */       try {
/* 301 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 303 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */ 
/* 307 */     return 0;
/*     */   }
/*     */ 
/*     */   private StringBuffer getCommonSQL(List<Object> params, String createUserId, int readFlag, Date startDate, Date endDate)
/*     */   {
/* 312 */     StringBuffer sql = new StringBuffer();
/* 313 */     if ((createUserId != null) && (createUserId.trim().length() > 0))
/*     */     {
/* 315 */       sql.append(" AND B.CreateUserId = ? ");
/* 316 */       params.add(createUserId);
/*     */     }
/*     */ 
/* 319 */     if (readFlag >= 0) {
/* 320 */       sql.append(" AND A.ReadFlag = ? ");
/* 321 */       params.add(Integer.valueOf(readFlag));
/*     */     }
/*     */     else {
/* 324 */       sql.append(" AND A.ReadFlag < 2");
/*     */     }
/*     */ 
/* 327 */     if (startDate != null) {
/* 328 */       sql.append(" AND DATEDIFF(B.UpdateDate, ?) >= 0 ");
/* 329 */       params.add(startDate);
/*     */     }
/* 331 */     if (endDate != null) {
/* 332 */       sql.append(" AND DATEDIFF(B.UpdateDate, ?) <= 0 ");
/* 333 */       params.add(endDate);
/*     */     }
/*     */ 
/* 336 */     return sql;
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> listAll(String receiveUserId, String createUserId, int readFlag, Date startDate, Date endDate, int offset, int pageSize, int companyId)
/*     */     throws DBException, SQLException
/*     */   {
/* 358 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 359 */     if (conn == null) {
/* 360 */       return null;
/*     */     }
/* 362 */     QueryRunner runner = new QueryRunner();
/* 363 */     StringBuffer sql = new StringBuffer();
/* 364 */     sql.append("SELECT A.MessageId, B.Title, B.Message, B.CreateDate, B.CreateUserId, A.ReadFlag FROM T_Message_Relation_Info AS A INNER JOIN T_Message_Body_Info AS B ON A.MessageId = B.MessageId WHERE A.CompanyId = ? AND A.ReceiveUserId = ? ");
/*     */ 
/* 366 */     List params = new ArrayList();
/* 367 */     params.add(Integer.valueOf(companyId));
/* 368 */     params.add(receiveUserId);
/*     */ 
/* 370 */     sql.append(getCommonSQL(params, createUserId, readFlag, startDate, 
/* 371 */       endDate));
/*     */ 
/* 373 */     sql.append(" ORDER BY A.UpdateDate DESC");
/* 374 */     if (pageSize > 0)
/* 375 */       sql.append(" LIMIT ").append(offset).append(", ").append(pageSize);
/*     */     try
/*     */     {
/* 378 */       return (List)runner.query(conn, sql.toString(), new MyMapListHandler(), 
/* 379 */         params.toArray());
/*     */     } finally {
/*     */       try {
/* 382 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 384 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int listAllCount(String receiveUserId, String createUserId, int readFlag, Date startDate, Date endDate, int companyId)
/*     */   {
/* 401 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 402 */     if (conn == null) {
/* 403 */       return 0;
/*     */     }
/* 405 */     QueryRunner runner = new QueryRunner();
/* 406 */     StringBuffer sql = new StringBuffer("SELECT COUNT(*) FROM T_Message_Relation_Info AS A INNER JOIN T_Message_Body_Info AS B ON A.MessageId = B.MessageId WHERE A.CompanyId = ? AND A.ReceiveUserId = ? ");
/* 407 */     List params = new ArrayList();
/* 408 */     params.add(Integer.valueOf(companyId));
/* 409 */     params.add(receiveUserId);
/* 410 */     sql.append(getCommonSQL(params, createUserId, readFlag, startDate, 
/* 411 */       endDate));
/*     */     try {
/* 413 */       Object obj = runner.query(conn, sql.toString(), 
/* 414 */         new ScalarHandler(1), params.toArray());
/* 415 */       if (!(obj instanceof Long)) {
/* 416 */         return 0;
/*     */       }
/* 418 */       return ((Long)obj).intValue();
/*     */     } catch (SQLException e) {
/* 420 */       logger.warn(e);
/*     */     } finally {
/*     */       try {
/* 423 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 425 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 428 */     return 0;
/*     */   }
/*     */ 
/*     */   public int updateAllViewStatus(String receiveUserId)
/*     */   {
/* 438 */     if ((receiveUserId == null) || (receiveUserId.trim().length() == 0)) {
/* 439 */       return 0;
/*     */     }
/* 441 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 442 */     if (conn == null) {
/* 443 */       return 0;
/*     */     }
/* 445 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 447 */       return runner.update(conn, "UPDATE T_Message_Relation_Info SET ReadFlag = 1, UpdateDate = NOW() WHERE ReadFlag = 0 AND ReceiveUserId = ?", 
/* 448 */         receiveUserId);
/*     */     } catch (SQLException e) {
/* 450 */       logger.warn("add message failed", e);
/*     */     } finally {
/*     */       try {
/* 453 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 455 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 458 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.MessageManager
 * JD-Core Version:    0.6.1
 */